package com.maigao.service;

import com.maigao.entity.form.AddCoachForm;
import com.maigao.entity.form.SearchCoachForm;
import com.maigao.entity.jpa.Coach;
import com.maigao.entity.jpa.CoachUser;
import com.maigao.entity.jpa.Student;
import com.maigao.entity.jpa.User;
import com.maigao.entity.vo.CoachUserVO;
import com.maigao.entity.vo.CoachVO;
import com.maigao.entity.vo.StudentCourseVO;
import com.maigao.entity.vo.StudentVO;
import com.maigao.persistent.CoachRepository;
import com.maigao.persistent.CoachUserRepository;
import com.maigao.persistent.StudentRepository;
import com.maigao.persistent.UserRepository;
import com.maigao.util.CollectionUtils;
import com.maigao.util.DateUtils;
import com.maigao.util.context.Context;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-21 23:01
 **/
@Service
public class CoachService {
    @Autowired
    private CoachRepository coachRepository;
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private CoachUserRepository coachUserRepository;

    public List<CoachUserVO> tryFindAll() {
        List<CoachUser> coachUsers = coachUserRepository.findAll();
        List<CoachUserVO> coachUserVOS = new ArrayList<>();
        for (CoachUser coachUser : coachUsers) {
            CoachUserVO coachUserVO = new CoachUserVO();
            BeanUtils.copyProperties(coachUser, coachUserVO);
            coachUserVO.setCreateTime(DateUtils.format(coachUser.getCreateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
            coachUserVOS.add(coachUserVO);
        }
        return coachUserVOS;
    }

    public Triple<Boolean, CoachVO, String> tryFindCoach(SearchCoachForm searchCoachForm) {
        Integer coachId = searchCoachForm.getCoachId();
        String coachName = searchCoachForm.getCoachName();
        String userUid = searchCoachForm.getUserUid();
        if (StringUtils.isAnyBlank(coachName, userUid) && null == coachId) {
            return Triple.of(false, null, "参数有误!");
        }
        Coach coach = null;
        if (null != coachId) {
            coach = coachRepository.findById(coachId);
        } else if (StringUtils.isNotBlank(coachName)) {
            coach = coachRepository.findByCoachName(coachName);
        } else if (StringUtils.isNotBlank(userUid)) {
            coach = coachRepository.findByUserUid(userUid);
        }
        if (null == coach) {
            return Triple.of(false, null, "未查询到相关数据!");
        }
        CoachVO coachVO = new CoachVO();
        BeanUtils.copyProperties(coach, coachVO);
        coachVO.setCreateTime(DateUtils.format(coach.getCreateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
        return Triple.of(true, coachVO, "");
    }

    public Pair<Boolean, String> tryAddCoach(AddCoachForm addCoachForm) {
        String coachName = addCoachForm.getCoachName();
        String mobile = addCoachForm.getMobile();
        Integer userType = addCoachForm.getUserType();
        if (StringUtils.isAnyBlank(coachName, mobile) || null == userType) {
            return Pair.of(false, "参数不完整!");
        }
        Coach oldCoach = coachRepository.findByMobile(mobile);
        if (null != oldCoach) {
            return Pair.of(false, "教练已存在!");
        }
        User.UserType userTypeEnum = User.UserType.getByValue(userType);
        if (null == userTypeEnum) {
            return Pair.of(false, "用户类型非法!");
        }
        User user = userService.initUser();
        user.setUserName(coachName);
        user.setMobile(mobile);
        user.setUserType(userType);
        String registrationCode = userService.createRegistrationCode(coachName, mobile, userType);
        user.setRegistrationCode(registrationCode);
        userRepository.save(user);
        Coach coach = new Coach();
        coach.setUserUid(user.getUid());
        coach.setCoachName(coachName);
        coach.setMobile(mobile);
        coach.setCreateTime(LocalDateTime.now());
        coach.setStatus(Coach.Status.NORMAL.getValue());
        coachRepository.save(coach);
        return Pair.of(true, registrationCode);
    }

    public Pair<Boolean, String> tryUpdateMobile(Integer coachId, String mobile) {
        Coach coach = coachRepository.findById(coachId);
        if (null == coach) {
            return Pair.of(false, "教练不存在!");
        }
        coachRepository.updateMobile(coachId, mobile);
        return Pair.of(true, "");
    }

    public Pair<Boolean, List<StudentVO>> findAllStudent() {
        Coach coach = getCurrentCoach();
        return findAllStudent(coach);
    }

    public Pair<Boolean, List<StudentVO>> findAllStudent(String uid) {
        Coach coach = coachRepository.findByUserUid(uid);
        return findAllStudent(coach);
    }

    public Pair<Boolean, List<StudentVO>> findAllStudent(Coach coach) {
        if (null == coach) {
            return Pair.of(false, null);
        }
        List<Student> students = studentRepository.findByCoachId(coach.getId());
        if (CollectionUtils.isEmpty(students)) {
            return Pair.of(false, null);
        }
        List<StudentVO> studentVOS = new ArrayList<>();
        for (Student student : students) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            studentVO.setCreateTime(DateUtils.format(student.getCreateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
            if (CollectionUtils.isNotEmpty(student.getStudentCourses())) {
                List<StudentCourseVO> studentCourses = new ArrayList<>();
                student.getStudentCourses().forEach(x -> {
                    StudentCourseVO item = new StudentCourseVO();
                    BeanUtils.copyProperties(x, item);
                    item.setStudentId(x.getId());
                    item.setCreateTime(DateUtils.format(x.getCreateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
                    item.setLastUpdateTime(DateUtils.format(x.getLastUpdateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
                    studentCourses.add(item);
                });
                studentVO.setStudentCourses(studentCourses);
            }
            studentVOS.add(studentVO);
        }
        return Pair.of(true, studentVOS);
    }


    public Pair<Boolean, List<CoachVO>> findAllCoachByStatus(Integer status) {
        List<Coach> coaches = coachRepository.findByStatus(status);
        if (CollectionUtils.isEmpty(coaches)) {
            return Pair.of(false, null);
        }
        List<CoachVO> coachVOS = new ArrayList<>();
        for (Coach coach : coaches) {
            CoachVO coachVO = new CoachVO();
            BeanUtils.copyProperties(coach, coachVO);
            coachVO.setCreateTime(DateUtils.format(coach.getCreateTime(), DateUtils.Formatter.DATETIME_YYYY_MM_DD_HH_MM_SS));
            coachVOS.add(coachVO);
        }
        return Pair.of(true, coachVOS);
    }

    private Coach getCurrentCoach() {
        com.maigao.util.context.user.User user = Context.current().getUser();
        String uid = user.getUid();
        return coachRepository.findByUserUid(uid);
    }

}
